<?php
// 本文档自动生成，仅供测试运行
class AgriculturalproductsCommonAction extends Action
{
	
	
	function _initialize() {
		
		//销售排行
		$this->getsalesamountList();
		
		if( !empty($_SESSION[C('USER_AUTH_KEY')]) ){
	    	$shoppingCondition['user_name'] =  $_SESSION[C('USER_AUTH_KEY')] ;
	    	$this->assign('shoppingCount',count($this->simpleQuery($shoppingCondition,'shoppingcart',0)));
	    }
	}
	
	
	
	/**
	 * 
	 * 最新上架
	 */
	public function getlastUpdateList(){
		$queryString_lastUpdate = "select * from product where status =1  order by statusdate desc  LIMIT 5";			
		$this->assign("lastUpdateList",$this->CommonSimplequery(1,$queryString_lastUpdate ,'' ,'' ,0,""));
	}
		
	
	/**
	 * 
	 * 查询购物车数量
	 */	
	public function getShoppingCount(){
		if( !empty($_SESSION[C('USER_AUTH_KEY')]) ){
	    	$shoppingCondition['user_name'] =  $_SESSION[C('USER_AUTH_KEY')] ;
	    	$this->assign('shoppingCount',count($this->simpleQuery($shoppingCondition,'shoppingcart',0)));
	    }
	}	    
	
	/**
	 * 
	 * 获取销售排行列表 
	 */ 
	public function getsalesamountList(){
		$queryString_salesamount = "select * from product where status =1  order by salesamount desc  LIMIT 5";			
		$this->assign("salesamountList",$this->CommonSimplequery(1,$queryString_salesamount ,'' ,'' ,0,""));
	}
	
	//新闻页面右边的展示
	public function rightNewsCommonQuery(){
		//最近更新
		$queryString_lastUpdate = "select * from news where status =1 order by statusdate desc limit 10"; 
		$lastUpdateList = $this->CommonSimplequery(1,$queryString_lastUpdate ,'' ,'' ,0,"") ;
		$this->assign('lastUpdateList',$lastUpdateList);
		
		//推荐阅读
		$queryString_recommend = "select * from news where status =1 and publishrecommend = 1 order by statusdate desc limit 10"; 
		$recommendList = $this->CommonSimplequery(1,$queryString_recommend ,'' ,'' ,0,"") ;
		$this->assign('recommendList',$recommendList);
	}

	public function getQueryPage($table,$condition,$orderField){
		$Dao = M($table);
		
	    // 计算总数
	    $count = $Dao->where($condition)->order($orderField)->count();
	    // 导入分页类
	    import("ORG.Util.Page");
	    // 实例化分页类
	    $p = new Page($count, 30);
	    // 分页显示输出
	    $page = $p->show();
	
	    // 当前页数据查询
	    $list = $Dao->where($condition)->order($orderField)->limit($p->firstRow.','.$p->listRows)->select();
	
	    // 赋值赋值
	    $this->assign('page', $page);
	  
	    $this->assign('singleList', $list);
	    
	  	
	}
	
	
	public function getCommonBlogQuery($code,$queryConditon,$all=1){
		//通过对应模块code 获取 pagecount和pagepanel的数量
		$moduleCondition['code'] = $code ;
		$moduleInfo = $this->CommonSimplequery($moduleCondition,"module",1) ;
		
		$Dao = M("blog");
		// 计算总数
		$count = $Dao->where($queryConditon)->count();
		// 导入分页类
		import("ORG.Util.Page");
		// 实例化分页类
		$p = new Page($count, $moduleInfo[pagecount]);
		// 分页显示输出
		$page = $p->show();
		
		// 当前页数据查询
		$list = $Dao->where($queryConditon)->order('createdate desc')->limit($p->firstRow.','.$p->listRows)->select();
		
		//如果是育儿博客查询
		if($all==1){
			//最近更新			
			$last_list = $Dao->where($queryConditon)->order('readtimes desc')->limit('0,'.$moduleInfo[panelcount])->select();	
			//我发布的blog
			$queryConditon['user'] =  Session::get(C('USER_AUTH_KEY')) ;
			
			//$condition['type']='blog';
			$my_last_list = $Dao->where($queryConditon)->order('createdate desc')->limit('0,'.$moduleInfo[panelcount])->select();
			$this->assign('last_list_info', $last_list);
			$this->assign('my_last_list', $my_last_list);
		}
		
		// 赋值赋值
		$this->assign('page', $page);
		$this->assign('list_info', $list);				
		$this->display() ;
	}
	
	
	/**
	 * 没有登陆时的跳转
	 * Enter description here ...
	 */
	public function redirectWhenNotLogin(){
		$this->assign("jumpUrl","__APP__/Register/login/");
		$this->success("您尚未登陆，无法进行操作！") ;
	}
	
	public function getIndexCssStyle($code){
		$conditon['code'] = $code ;
		$moduleInfo = $this->CommonSimplequery($conditon,'module',1) ;
		$this->assign("moduleInfo",$moduleInfo);
	}
	
	
	
	/**
	 * 简单查询
	 * Enter description here ...
	 * @param unknown_type $condition
	 * @param unknown_type $tableName
	 * @param unknown_type $patten  如果为1，则只查询一条数据。为0，查询多条数据,默认为0
	 */
	public function CommonSimplequery($isDIY,$queryString,$condition,$tableName,$patten=0,$orderField){

		$model = M ( $tableName );		
		if($isDIY == 1 ){
			return $model->query($queryString);
		}else{
			if($patten==1){
				if( !empty($orderField) ){
					return $model->where($condition)->order($orderField)->find();
				}else{
					return $model->where($condition)->find();	
				}			
			}else{
				if( !empty($orderField) ){
					return $model->where($condition)->order($orderField)->select();
				}else{
					return $model->where($condition)->select();	
				}			
			}
		}
	}
	
	
	public function simpleQuery($condition,$tableName,$patten=0){

		$model = M ( $tableName );
		if($patten==1){
			return $model->where($condition)->find();
		}else{
			return $model->where($condition)->select();
		}
	}


	public function localUpdate($table,$condition,$data){
		$Dao = M($table);
		$result = $Dao->where($condition)->save($data);
		return $result != false ? $result : $Dao.getError () ;
	}


	/**
	 *
	 * Enter description here ...
	 * @param unknown_type $module
	 * @param unknown_type $table
	 * @param unknown_type $list_prefix    _video
	 */
	public function getCommonQueryPage($module,$table,$list_prefix,$sub_module=0,$publishindex=0){
		$Dao = M($table);
		// 计算总数
		
		$tempmodule = $module ;
		
		if($publishindex==1){
			$tempmodule = $module . C(INDEX_PREFIX) ;
		}
		$codeTemp = "" ;		
		if($sub_module==1){
			$codeTemp = "subcode" ;
		}else if($sub_module==0){
			$codeTemp = "code" ;
		}
		$count = $Dao->where($codeTemp.' = '.'"' . $module . '"' )->count();	
		//通过对应模块code 获取 pagecount和pagepanel的数量
		$moduleCondition['code'] = $tempmodule ;
		$moduleInfo = $this->CommonSimplequery($moduleCondition,"module",1) ;
		
		// 导入分页类
		import("ORG.Util.Page");
		// 实例化分页类
		
		$p = new Page($count, $moduleInfo[pagecount]);
		// 分页显示输出
		$page = $p->show();
		// 当前页数据查询
		
		$list = $Dao->where($codeTemp.' = '.'"' . $module . '"')->order('createdate desc')->limit($p->firstRow.','.$p->listRows)->select();
		//最近更新,,对应于每个新闻块，比如首页的新闻动态块
		$last_list = $Dao->where($codeTemp.' = '.'"' . $module . '"')->order('createdate desc')->limit('0,'.$moduleInfo[panelcount] )->select();
		// 赋值赋值
		
		$this->assign('page', $page);
		$this->assign('list_info'.$list_prefix, $list);
		$this->assign('last_update_list'.$list_prefix, $last_list);
		
	}


	public function detail($table){
		$model = M ( "Bulletin" );
		$id = $_REQUEST ['id'];
		$condition['id'] = $id;
		// 查询数据
		$list = $model->where($condition)->find();
		//dump($list) ;
		$this->assign ( "info", $list );
		$this->display() ;
	}

	/**
	 * 按主健进行删除
	 * Enter description here ...
	 * @param unknown_type $table
	 * @param unknown_type $id
	 */
	public function delete($table,$id){
		$Dao = M($table);
		$result = $Dao->where('id = '.$id)->delete();
		return $result != false ? $result : $Dao.getError () ;
	}


}
?>